from tokenize import String

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base

from Connect import session

Base = declarative_base()
Base.query = session.query_property()


# 定义数据库类
class OldAirconditionInfo(Base):
    # 表的名字
    __tablename__ = 'AirconInfo'
    # 表的结构
    id = Column(Integer, primary_key=True)
    address = Column(String(20))
    gatewayId = Column(String(20))
    gatewayPort = Column(String(20))
    name = Column(String(20))
    roomId = Column(String(20))
    fullName = Column(String(20))

    def __str__(self):
        pass

    def __init__(self, id=None, address=None, gatewayId=None, gatewayPort=None, name=None, roomId=None, fullName=None):
        self.id = id
        self.address = address
        self.gatewayId = gatewayId
        self.gatewayPort = gatewayPort
        self.name = name
        self.roomId = roomId
        self.fullName = fullName


class NewAirconditionInfo(Base):
    # 表的名字
    __tablename__ = 'aircon_info'
    # 表的结构
    id = Column(Integer, primary_key=True, autoincrement=False)
    address = Column(String(20))
    port_id = Column(String(20))
    room_id = Column(String(20))
    name = Column(String(20))

    def __str__(self):
        pass

    def __init__(self, id=None, address=None, port_id=None, room_id=None, name=None):
        self.id = id
        self.address = address
        self.port_id = port_id
        self.room_id = room_id
        self.name = name


class NewComputerPort(Base):
    # 表的名字
    __tablename__ = 'computer_port'
    # 表的结构
    id = Column(Integer, primary_key=True)
    computer_id = Column(String(20))
    port = Column(String(20))

    def __str__(self):
        pass

    def __init__(self, id=None, computer_id=None, port=None):
        self.id = id
        self.address = computer_id
        self.port = port


if __name__ == '__main__':
    oldAirconditionInfoList = session.query(OldAirconditionInfo).all()
    for oldAirconditionInfo in oldAirconditionInfoList:
        print(oldAirconditionInfo.fullName+'  '+str(oldAirconditionInfo.gatewayId)+'  '+str(oldAirconditionInfo.gatewayPort))
        newComputerPort = session.query(NewComputerPort).filter(
            NewComputerPort.computer_id == oldAirconditionInfo.gatewayId,
            NewComputerPort.port == oldAirconditionInfo.gatewayPort
        ).one()
        newAirconditionInfo = NewAirconditionInfo(
            oldAirconditionInfo.id,
            oldAirconditionInfo.address,
            newComputerPort.id,
            oldAirconditionInfo.roomId,
            oldAirconditionInfo.name
        )
        session.add(newAirconditionInfo)
    session.commit()
